import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

public class CalcularPeso {
    private static PesoControl control = new PesoControl();
    private static JPanel pPlaca, pCodFilme, pSaida;
    private static JTextField placa, codFilme, saida;
    private static JButton button;
    private static JComboBox op;
    private static JTextField peso;
    private static JPanel pPeso;
    private static JTextField km;
    private static JPanel pKm;
    
    public CalcularPeso() {
    }
    
    public JPanel novoForm(){
        JPanel form = new JPanel();
        form.setLayout(new FlowLayout());
       
        JPanel panel = new JPanel(new GridLayout(6,1));
        form.add(panel);
        
        String[] array = new String[4];
        array[0] = "Saída";
        array[1] = "Entrada";
        array[2] = "Apagar Entradas Não Finalizadas";
        JComboBox choice = new JComboBox(array);
        op = choice;
        panel.add(choice);
        ChoiceListener itemListener = new ChoiceListener() {};
        choice.addItemListener(itemListener);
      
        JPanel row = new JPanel();
        pPlaca = row;
        JLabel label = new JLabel("Placa Caminhão: "); // construct Label
        row.add(label);
        JTextField text = new JTextField("XXX0000", 10);
        placa = text;
        row.add(text);
        panel.add(row);
        
        row = new JPanel();
        pPeso = row;
        label = new JLabel("Peso Balança: "); // construct Label
        row.add(label);
        text = new JTextField("0", 10);
        peso = text;
        row.add(text);
        panel.add(row);
        
        row = new JPanel();
        pKm = row;
        label = new JLabel("Quilometragem do Caminhão: "); // construct Label
        row.add(label);
        text = new JTextField("0", 10);
        km = text;
        row.add(text);
        panel.add(row);

        button = new JButton("Calcular Peso"); // construct Button
        panel.add(button);
        CalcularPeso.ButtonListener buttonListener = new CalcularPeso.ButtonListener();
        button.addActionListener(buttonListener);

        row = new JPanel();
        pSaida = row;
        text = new JTextField(10);
        text.setEditable(false);
        saida = text;
        row.add(text);
        panel.add(row);
        pSaida.setVisible(false);
        
        form.setSize(300, 300);
        form.setVisible(true);
        return form;
    }
    
    private static class ChoiceListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent ie) {
            button.setLabel("Calcula Peso "+ op.getSelectedItem().toString());
            if(op.getSelectedIndex() == 2)
                button.setLabel(op.getSelectedItem().toString());
        }
    }
    
    private static class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            String p = placa.getText();
            int pe = Integer.parseInt(peso.getText());
            int k = Integer.parseInt(km.getText());
            if(op.getSelectedIndex() == 0)
                control.calculaSaida(p, pe, k);
            else if(op.getSelectedIndex() == 1)
                control.calculaEntrada(p, pe, k);
            else control.limparEntradas();
        }
    }
   
}